﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

namespace kindly
{
    public partial class Send : LoggingPage
    {
        string jsonRequestString, result;
        JSONRequest request = null;
        string filePath;

        protected void Page_Load(object sender, EventArgs e)
        {
            using (StreamReader reader = new StreamReader(Request.InputStream))
            {
                jsonRequestString = reader.ReadToEnd();
            }

            if (parse())
            {
                try
                {
                    Mail.Send(filePath, request.Recipients, false);
                    result = "OK";
                }
                catch (Exception ex)
                {
                    result = string.Format("Error occured while sending: {0}", ex.Message);
                }                    
            }

            // write JSON result
            JSONSimpleResult jsonresult = new JSONSimpleResult
            {
                Result = result
            };
            jsonresult.WriteResponse();
        }

        private bool parse()
        {
            if (string.IsNullOrEmpty(jsonRequestString))
            {
                result = "Empty request data!";
                return false;
            }
            try
            {
                request = fastJSON.JSON.Instance.ToObject<JSONRequest>(jsonRequestString);
            }
            catch (Exception ex)
            {
                result = "Error deserializing JSON data: " + ex.Message;
                return false;
            }
            if (request == null)
            {
                result = "Empty JSON data!";
                return false;    
            }

            if (string.IsNullOrEmpty(request.Filename))
            {
                result = "No file specified!";            
                return false;
            }

            if (request.Recipients.Length < 1)
            {
                result = "No recipients specified!";
                return false;
            }
            if (request.Recipients.Length > 100)
            {
                result = "Too many recipients specified!";
                return false;
            }

            filePath = Files.GetFilePath(request.Filename);
            if (!File.Exists(filePath))
            {
                result = "File does not exist: " + Path.GetFileName(filePath);
                return false;
            }

            return true;
        }

        public class JSONRequest
        {
            private string filename;
            private string[] recipients;

            public string Filename
            {
                get { return filename; }
                set { filename = value; }
            }            

            public string[] Recipients
            {
                get { return recipients; }
                set { recipients = value; }
            }
        }

    }
}